#include <memory>
#include <QGraphicsTextItem>
#include "mainwindow.h"
#include "ui_mainwindow.h"

using namespace std;


const unsigned int MainWindow::CELL_SIZE=Tile::TILE_SIZE;
const unsigned int MainWindow::LINE_THICKNESS=1;
const unsigned int MainWindow::SIZE=CELL_SIZE+LINE_THICKNESS;
const unsigned int MainWindow::EVOLUTION_LIMIT=1000;
const QPen MainWindow::CELL_PEN=QPen(Qt::black,LINE_THICKNESS);
const QColor MainWindow::WHITE_COLOUR=Qt::white;
const QColor MainWindow::BLACK_COLOUR=Qt::black;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->graphicsView->setScene(&scene);

    auto grid=make_shared<Grid>();
    grid->build(450,450);
    ca=make_shared<EpidemicCA>(grid,0.5,0.1);
    ca->setParam(0.0f,0,0);
    evolution.push_back(ca->current_states->copy());
    step=0;
    repaint();
    ui->graphicsView_2->setScene(&scene2);
    auto l=scene2.addText("LEGEND:");
    l->setPos(-100,-150);


    l=scene2.addText("DEAD");
    l->setPos(20,-90);
    scene2.addRect(-100,-100,30,30,QPen(),QBrush(Qt::black));

    l=scene2.addText("SUSCEPTIBLE");
    l->setPos(20,-40);
    scene2.addRect(-100,-50,30,30,QPen(),QBrush(Qt::white));
    QLinearGradient lg(-100, 30, 100, 30);
    lg.setColorAt(0.0, QColor::fromRgb(255,0,0));
    lg.setColorAt(1.0, QColor::fromRgb(50,0,0));
    scene2.addRect(-100,0,100,30,QPen(),lg);
    l=scene2.addText("INFECTED");
    l->setPos(20,10);
    lg.setColorAt(0.0, QColor::fromRgb(0,255,0));
    lg.setColorAt(1.0, QColor::fromRgb(0,50,0));
    scene2.addRect(-100,50,100,30,QPen(),lg);
    l=scene2.addText("IMMUNE");
    l->setPos(20,60);




}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::repaint()
{
    scene.clear();
    auto g=evolution.at(step);
    QBrush brush;
    for(int i=0;i<g->height();++i)
        for(int j=0;j<g->width();++j)
        {

            if(g->tiles[i][j]->value==-1)brush=BLACK_COLOUR;
            else if(g->tiles[i][j]->value==0)brush=WHITE_COLOUR;
            else if(g->tiles[i][j]->value<=ca->a)
            {
                auto s1=(ca->a==1)?255:255-roundf(205/(ca->a-1))*(g->tiles[i][j]->value-1);
                brush=QColor::fromRgb(s1,0,0);
            }
            else
            {
                auto s2=(ca->b==1)?255:255-roundf(205/(ca->b-1))*(g->tiles[i][j]->value-ca->a-1);
                brush=QColor::fromRgb(0,s2,0);
            }
            scene.addRect(j*SIZE,i*SIZE,CELL_SIZE,CELL_SIZE,CELL_PEN,brush);
        }

    ui->info->setText("step: "+QString::number(step)+
    "     evolution limit: "+QString::number(EVOLUTION_LIMIT-1));
}

void MainWindow::update()
{
    /*
    if(step>=evolution.size()-1){
        if(timer.isActive())timer.stop();
        ui->startButton->setEnabled(true);
        ui->nextButton->setEnabled(true);
        ui->prevButton->setEnabled(true);
        return;
    }

    ++step;
    repaint();
    */

}
/*
void MainWindow::on_challengeButton_clicked()
{

    connect(&timer, SIGNAL(timeout()), this, SLOT(update()));
    ui->startButton->setEnabled(false);
    ui->nextButton->setEnabled(false);
    ui->prevButton->setEnabled(false);
    timer.start(30);

}

void MainWindow::on_nextButton_clicked()
{

    update();

}


void MainWindow::on_resetButton_clicked()
{

    if(timer.isActive())
    {
        timer.stop();
        ui->startButton->setEnabled(true);
        ui->nextButton->setEnabled(true);
        ui->prevButton->setEnabled(true);


    }
    step=0;
    repaint();

}
*/


void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{
    /*
    float proportion;
    int a,b;
    bool ok;
    a=ui->aLineEdit->text().toInt(&ok);
    if(!ok)return;
    b=ui->bLineEdit->text().toInt(&ok);
    if(!ok)return;
    proportion=ui->pLineEdit->text().toFloat(&ok);
    if(!ok)return;
    ca->setParam(proportion,a,b);
    evolution.clear();
    evolution.push_back(ca->current_states->copy());
    while(ca->next() && ca->step<EVOLUTION_LIMIT)evolution.push_back(ca->current_states->copy());
    step=0;
    repaint();
    ui->aLineEdit->setText(QString::number(ca->a));
    ui->bLineEdit->setText(QString::number(ca->b));
    ui->pLineEdit->setText(QString::number(ca->proportion));

      */
}

void MainWindow::on_nextButton_clicked()
{

}

void MainWindow::on_challengeButton_clicked()
{

}

void MainWindow::on_firstButton_clicked()
{

}
